package com.gm.wj.controller;

import com.gm.wj.entity.SysMessage;
import com.gm.wj.result.Result;
import com.gm.wj.result.ResultFactory;
import com.gm.wj.service.SysMessageService;
import com.gm.wj.util.SendByEmailTools;
import com.gm.wj.util.SysLoging;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.SendFailedException;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: wj
 * @description: 信息发送
 * @author: Huang_shengjun
 * @create: 2020-11-11 17:17
 **/
@Log4j2
@RestController
@Api(tags = "信息发送相关接口")
public class SendMessageController {

    @Autowired
    SysMessageService sysMessageService;

    // 邮箱发送
    @PostMapping("/api/admin/message/send")
    @SysLoging("发送邮箱信息") //根据SysLoging的注解信息，进行方法的注释
    @ApiOperation("发送邮箱信息的接口")
    public Result sendEmail(@RequestBody @Valid SysMessage sysMessage, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return ResultFactory.buildFailResult(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        switch (sysMessageService.verifyCode(sysMessage)) {
            case 1:
                return ResultFactory.buildFailResult("邮箱格式错误！");
            case 3:
                return ResultFactory.buildFailResult("手机号格式错误！");
            case 4:
                return ResultFactory.buildFailResult("未选择信息类型！");
            default:
        }

        try {

            switch (sysMessage.getSendType()) {
                case 0:
                    if (sysMessage.getSubject().length() <= 0) {
                        log.info("主题不能为空");
                        return ResultFactory.buildFailResult("主题不能为空");
                    }
                    if (sysMessage.getFilename().length() > 0) {
                        if (sysMessageService.sendMailAndAppendix(sysMessage) == "1") {
                            log.info("sendMailAndAppendix发送成功");
                            return ResultFactory.buildSuccessResult("发送成功",null);
                        }
                    } else {
                        if (sysMessageService.sendEmail(sysMessage) == "1") {
                            log.info("sendMail发送成功");
                            return ResultFactory.buildSuccessResult("发送成功",null);
                        }
                    }
                    log.info("发送失败");
                    return ResultFactory.buildFailResult("发送失败");
                case 1:
                    String message = sysMessageService.sendPhone(sysMessage);
                    if ( message == "1") {
                        log.info("sendPhone发送成功");
                        return ResultFactory.buildSuccessResult("发送成功",null);
                    }
                    log.info("发送失败");
                    return ResultFactory.buildFailResult(message);
                default:
                    return ResultFactory.buildFailResult("发送失败");
            }

        } catch (Exception e) {
            e.printStackTrace(); // 在控制台打印出异常种类，错误信息和出错位置等
            return ResultFactory.buildFailResult("服务器出错了，请联系管理员。错误信息：" + e.getMessage());
        }
    }

    // 列表
    @PostMapping("/api/admin/message/{size}/{page}")
    @ApiOperation("查询通知列表信息的接口")
    @SysLoging("查询通知列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result listMessage(@PathVariable("size") int size, @PathVariable("page") int page,String queryKey1,String queryKey2, String queryKey3, String queryKey4, String queryKey5) {
        try{
            return ResultFactory.buildSuccessResult("查询成功",sysMessageService.list(page - 1, size,queryKey1,queryKey2,queryKey3,queryKey4,queryKey5));
        } catch (Exception e){
            e.printStackTrace(); // 在控制台打印出异常种类，错误信息和出错位置等
            return ResultFactory.buildFailResult("服务器出错了，请联系管理员。错误信息：" + e.getMessage());
        }
    }

    // 根据条件查询列表
    @PostMapping("/api/admin/message/findByQueryKey")
    @ApiOperation("根据条件查询通知列表的接口")
    @SysLoging("根据条件查询通知列表")
    public Result listByQueryKey(String queryKey1,String queryKey2, String queryKey3, String queryKey4, String queryKey5) {
        try{
            return ResultFactory.buildSuccessResult("查询成功",sysMessageService.listByQueryKey(queryKey1,queryKey2,queryKey3,queryKey4,queryKey5));
        }catch (Exception e){
            e.printStackTrace(); // 在控制台打印出异常种类，错误信息和出错位置等
            return ResultFactory.buildFailResult("服务器出错了，请联系管理员。错误信息：" + e.getMessage());
        }
    }

}
